
import snowflake.client
from flask import Blueprint,request,jsonify
import json
from util.myredis import r

from models.user import *

grab_blue = Blueprint("grabblue",__name__)
@grab_blue.route('/getgrabproject')
def getgrabproject():
    p1 = db.session.query(project).filter(project.status==1)
    p2 = db.session.query(project1).filter(project1.status==1)
    p3 = db.session.query(project2).filter(project2.status==1)
    sql = p1.union(p2).union(p3)
    list = []
    for i in sql:
        list.append({
            'code': str(i.code),
            'name': i.name,
            'title': i.title,
            'descs': i.descs,
            'type': i.type,
            'money': i.money,
            'mobile': i.mobile,
            'trade': i.trade,
            'status': i.status,
            'userid': i.userid,
            'create_time': str(i.create_time),
            'update_time': str(i.update_time)
        })
    return jsonify({'code':200,'data':list})


@grab_blue.route('/postgrab',methods=['POST'])
def postgrab():
    data = json.loads(request.data)
    code = data['code']
    qiangid = data['userid']
    tablename = 'project'
    number = hash(code) % 3
    if number != 0:
        tablename = tablename + str(number)
    tablename1 = globals().get(f'{tablename}')
    try:
        pro = tablename1.query.filter_by(code=code).first()
        userid = pro.userid
        tablename1.query.filter_by(code=code).update({'status': 3})
        db.session.commit()
        gr = grab(code=code,qiangid=qiangid,userid=userid,status=1)
        db.session.add(gr)
        db.session.commit()
    except:
        return jsonify({'code': 200,'msg':'抢失败'})
    return jsonify({'code': 200,'msg':'抢单成功'})

# 展示抢单成功的用户
@grab_blue.route('/empoloyer')
def empoloyer():
    userid = request.args.get('userid')
    gr = grab.query.filter_by(userid=userid,status=1).all()
    list = []
    for i in gr:
        list.append({
            'id':i.id,
            'code':str(i.code),
            'qiangid':i.qiangid
        })
    return jsonify({'code':200,'data':list})

# 雇主确认
@grab_blue.route('/upempoloyer')
def upempoloyer():
    code = request.args.get('code')
    grab.query.filter_by(code=code).update({'status': 2})
    db.session.commit()
    return jsonify({'code':200,'msg':'雇主确认'})

# 展示雇主同意的订单
@grab_blue.route('/aggreempoloyer')
def aggreempoloyer():
    userid = request.args.get('userid')
    gr = grab.query.filter_by(qiangid=userid,status=2).all()
    list = []
    for i in gr:
        list.append({
            'id':i.id,
            'code':str(i.code),
            'qiangid':i.qiangid
        })
    return jsonify({'code':200,'data':list})


# 项目完成确认
@grab_blue.route('/accempoloyer')
def accempoloyer():
    code = request.args.get('code')
    grab.query.filter_by(code=code).update({'status': 3})
    db.session.commit()
    return jsonify({'code':200,'msg':'项目完成'})

# 展示项目完成的订单
@grab_blue.route('/projectempoloyer')
def projectempoloyer():
    userid = request.args.get('userid')
    gr = grab.query.filter_by(userid=userid,status=3).all()
    list = []
    for i in gr:
        list.append({
            'id':i.id,
            'code':str(i.code),
            'qiangid':i.qiangid
        })
    return jsonify({'code':200,'data':list})


# 打款
@grab_blue.route('/remit')
def remit():
    code = request.args.get('code')
    # try:
    grab.query.filter_by(code=code).update({'status': 4})
    db.session.commit()
    gr = grab.query.filter_by(code=code).first()
    qiangid = gr.qiangid
    print('qiang',qiangid)
    userid = gr.userid
    print('userid',userid)

    # tablename = 'project'
    # number = hash(code) % 3
    # if number != 0:
    #     tablename = tablename + str(number)
    # tablename = globals().get(f'{tablename}')
    # 报错中?????????????????????????????????????
    # pro = tablename.query.filter_by(code=code).first()
    pro = db.session.execute("select * from (select * from project where status=3 union all select * from project1 where status=3 union all select * from project2 where status=3)t where t.code=code")
    proje = list(pro)
    for i in proje:
        print(i)
        money = i.money
        user = User.query.filter_by(id=userid).first()
        print('user',user.id)
        lmoney = user.lockmoney
        print('23',lmoney)
        lockmoney = lmoney - money
        print('lockmoney',lockmoney)
        User.query.filter_by(id=userid).update({'lockmoney':lockmoney})
        print(1)
        db.session.commit()
        qiang = User.query.filter_by(id=qiangid).first()
        print(qiang.id)
        print('qiangssss',qiang.money)
        tmoney = qiang.money+money
        print('????',tmoney)
        User.query.filter_by(id=qiangid).update({'money':tmoney})
        print(2)
        db.session.commit()
# except:
    #     return jsonify({'code':400,'msg':'打款失败'})

        return jsonify({'code':200,'msg':'打款成功'})



# def aa():
#     sql = project.query.filter_by(code=4733334238588309505).first()
#     print(sql)
#     print(sql.code)
#
# aa()







